package net.imagine.backend.mockrmi.remote.provider.mina.server;

import java.net.InetSocketAddress;

import net.imagine.backend.mockrmi.remote.protocol.ResponseBean;
import net.imagine.backend.mockrmi.util.MockRmiUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * Mina实现的服务端IO处理类
 * 
 * @author wuqh
 * @see IoHandlerAdapter
 * @see MockRmiUtils#handlerMessage(Object, InetSocketAddress)
 * */
public class MinaServerIoHandler extends IoHandlerAdapter {
	private static final Log logger = LogFactory.getLog(MinaServerIoHandler.class);

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		logger.error("server occurred an unexpected excpetion", cause);
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		logger.debug("Server:message received");
		InetSocketAddress addr = (InetSocketAddress) session.getLocalAddress();
		ResponseBean response = MockRmiUtils.handlerMessage(message, addr);
		session.write(response);
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		logger.debug("Server:message sended");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		logger.debug("Server:session closed");
	}

}
